package com.ruoyi.project.modules.business.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.project.modules.businessUsage.domain.BusinessUsage;
import com.ruoyi.project.modules.businessUsage.service.IBusinessUsageService;
import com.ruoyi.project.modules.interfaceObject.domain.InterfaceObject;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.service.IInterfaceInfoService;
import com.ruoyi.project.modules.page.domain.Page;
import com.ruoyi.project.modules.usage.domain.Usage;
import com.ruoyi.project.modules.usage.service.IUsageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.business.domain.BusinessInfo;
import com.ruoyi.project.modules.business.service.IBusinessInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 业务需求信息Controller
 *
 * @author smallrain
 * @date 2021-10-28
 */
@Controller
@RequestMapping("/business/businessInfo")
public class BusinessInfoController extends BaseController {
    private String prefix = "business/businessInfo";

    @Autowired
    private IBusinessInfoService businessInfoService;

    @RequiresPermissions("business:businessInfo:view")
    @GetMapping()
    public String businessInfo() {
        return prefix + "/businessInfo";
    }

    /**
     * 查询业务需求信息列表
     */
    @RequiresPermissions("business:businessInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusinessInfo businessInfo) {
        startPage();
        businessInfo.setUserId(ShiroUtils.getSysUser().getUserId());
        businessInfo.setFlag(this.getFlag());
        List<BusinessInfo> list = businessInfoService.selectBusinessInfoList(businessInfo);
        return getDataTable(list);
    }

    /**
     * 导出业务需求信息列表
     */
    @RequiresPermissions("business:businessInfo:export")
    @Log(title = "业务需求信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusinessInfo businessInfo) {
        List<BusinessInfo> list = businessInfoService.selectBusinessInfoList(businessInfo);
        ExcelUtil<BusinessInfo> util = new ExcelUtil<BusinessInfo>(BusinessInfo.class);
        return util.exportExcel(list, "业务需求信息数据");
    }

    @Autowired
    private IBusinessUsageService businessUsageService;

    @Autowired
    private IUsageService usageService;

    /**
     * 新增业务需求信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        Usage usage = new Usage();
        List<Usage> usageInfoList = this.usageService.selectUsageList(usage);
        mmap.put("usageInfoList", usageInfoList);
        return prefix + "/add";
    }

    /**
     * 新增保存业务需求信息
     */
    @RequiresPermissions("business:businessInfo:add")
    @Log(title = "业务需求信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusinessInfo businessInfo) {
        return toAjax(businessInfoService.insertBusinessInfo(businessInfo));
    }

    /**
     * 修改业务需求信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BusinessInfo businessInfo = businessInfoService.selectBusinessInfoById(id);
        mmap.put("businessInfo", businessInfo);

        Usage usage = new Usage();
        List<Usage> usageInfoList = this.usageService.selectUsageList(usage);
        mmap.put("usageInfoList", usageInfoList);

        BusinessUsage businessUsage = new BusinessUsage();
        businessUsage.setBusinessId(id);
        List<BusinessUsage> businessUsages = this.businessUsageService.selectBusinessUsageList(businessUsage);
        if (CollectionUtil.isNotEmpty(businessUsages)) {
            List<Long> usageIds = new ArrayList<>();
            for (BusinessUsage businessUsage1 : businessUsages) {
                usageIds.add(businessUsage1.getUsageId());
            }
            mmap.put("usageIds", usageIds);
        }
        return prefix + "/edit";
    }

    /**
     * 修改保存业务需求信息
     */
    @RequiresPermissions("business:businessInfo:edit")
    @Log(title = "业务需求信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusinessInfo businessInfo) {
        return toAjax(businessInfoService.updateBusinessInfo(businessInfo));
    }

    /**
     * 删除业务需求信息
     */
    @RequiresPermissions("business:businessInfo:remove")
    @Log(title = "业务需求信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(businessInfoService.deleteBusinessInfoByIds(ids));
    }

    @GetMapping("/detail/{id}")
    public String details(@PathVariable("id") Long id, ModelMap mmap) {
        BusinessInfo businessInfo = this.businessInfoService.selectBusinessInfoById(id);
        mmap.put("businessInfo", businessInfo);
        return prefix + "/details";
    }

    @PostMapping("/usageList")
    @ResponseBody
    public AjaxResult usageList(@RequestParam Long businessId) {
        BusinessUsage businessUsage = new BusinessUsage();
        businessUsage.setBusinessId(businessId);
        List<BusinessUsage> businessUsages = this.businessUsageService.selectBusinessUsageList(businessUsage);
        if (CollectionUtil.isNotEmpty(businessUsages)) {
            Set<Long> usageIds = businessUsages.stream().map(BusinessUsage::getUsageId).collect(Collectors.toSet());
            Usage usage = new Usage();
            usage.setUsageIds(usageIds);
            List<Usage> usageList = this.usageService.selectUsageList(usage);
            return AjaxResult.success(usageList);
        }
        return AjaxResult.success(new ArrayList<Usage>());
    }
}
